var express = require('express');
var router = express.Router();
var User = require('../models/user')
let crypto = require('crypto')

var cryptoPassFunc = function(password) {
    const md5 = crypto.createHash('md5');
    return md5.update(password).digest('hex');
};



//测试数据添加
router.get('/save', function (req, res, next) {

    const hash = crypto.createHash('md5');
    var password=cryptoPassFunc('123456');
    var myobj = [
        { userName: 'kwor', realName: 'kwor',password: password,email: 'kwor@163.com',phone: '13221952583' },
        { userName: 'admin', realName: 'admin',password: password,email: 'admin@163.com',phone: '13221952581' }
    ];

    User.insertMany(myobj, function (err, result) {
        // if (err) throw err;
        if (err) {
            res.json({
                status: '1',
                msg: err.message
            })
        } else {
            res.json({
                status: '0',
                msg: '',
                result: {
                    count: result.insertedCount,
                    list: result
                }
            })
        }

    });


});


//getClassList为二级路由，
router.get('/get', function (req, res, next) {
    User.find({}, function (err, doc) {
        if (err) {
            res.json({
                status: '1',
                msg: err.message
            })
        } else {
            res.json({
                status: '0',
                msg: '',
                result: {
                    count: doc.length,
                    list: doc
                }
            })
        }
    })
});

module.exports = router;